package com.whlilong.jxc.editor.support;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Khinfo;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.ExtentionInfo;

public class ClientInfoEditorSupport extends EditingSupport implements
		ITextKeyChangeListener {

	/**
	 * 当前第几列
	 */
	private int currentColumn;

	/**
	 * 是否第一次修改该单元格
	 */
	private boolean isFirst = true;

	/**
	 * 为单元格添加的editor
	 */
	private CellEditor editor;

	/**
	 * 客户类别list
	 */
	private List<Kstype> merchantTypelist = new ArrayList<Kstype>();

	/**
	 * 客户区域list
	 */
	private List<Keshang> districtList = new ArrayList<Keshang>();

	/**
	 * Combo初始化
	 */
	private String[] merchantItems,districtItems;

	@SuppressWarnings("unchecked")
	public ClientInfoEditorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		comboBoxInitial();
		this.currentColumn = column;
		switch (column) {
		case 2:
		case 3:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
			editor = new ClientInfoTextKeyCellEditor(viewer.getTable(),
					listener, column, this);
			break;
		case 5:
			editor = new ClientInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, districtItems);
			break;
		case 6:
			editor = new ClientInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, merchantItems);
			break;
		default:
			editor = null;
		}
	}

	private void comboBoxInitial() {
		merchantTypelist = ExtentionInfo.getInstance().getKstypeinfo();
		merchantItems = new String[merchantTypelist.size() + 1];
		merchantItems[0] = "不定";
		for (int i = 0; i < merchantTypelist.size(); i++) {
			merchantItems[i + 1] = merchantTypelist.get(i).getKstype();
		}

		districtList = ExtentionInfo.getInstance().getKeshanginfo();
		districtItems = new String[districtList.size() + 1];
		districtItems[0] = "不定";
		for (int i = 0; i < districtList.size(); i++) {
			districtItems[i + 1] = districtList.get(i).getKeshangarea();
		}		
	}

	@Override
	protected boolean canEdit(Object element) {
		// TODO Auto-generated method stub
		return (currentColumn != 1 && currentColumn != 4);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		// TODO Auto-generated method stub
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Khinfo obj = (Khinfo) element;
		switch (currentColumn) {
		case 2:
			String id = obj.getKhid();
			if (isFirst) {
				((ClientInfoTextKeyCellEditor) editor).setOldString(id);
				isFirst = false;
			}
			return id;
		case 3:
			return obj.getKhname();
		case 4:
			return obj.getPym();
		case 5:
			for (int i = 0; i < districtList.size(); i++)
				if (obj.getKhareaname().equals(
						districtList.get(i).getKeshangarea()))
					return i + 1;
			return 0;
		case 6:
			for (int i = 0; i < merchantTypelist.size(); i++)
				if (obj.getKhtpname().equals(
						merchantTypelist.get(i).getKstype()))
					return i + 1;
			return 0;
		case 7:
			return obj.getKhtel();
		case 8:
			return obj.getKhaddress();
		case 9:
			return obj.getPeople();
		case 10:
			return obj.getFax();
		case 11:
			return obj.getYzbm();
		case 12:
			return obj.getMail();
		case 13:
			return obj.getYh();
		}

		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Khinfo obj = (Khinfo) element;
		switch (currentColumn) {
		case 2:
			obj.setKhid(String.valueOf(value));
			break;
		case 3:
			obj.setKhname(String.valueOf(value));
			break;
		case 4:
			obj.setPym(String.valueOf(value));
			break;
		case 5:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setKhareaname("不定");
			for (int i = 0; i < districtList.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setKhareaname(districtList.get(i).getKeshangarea());
			break;
		case 6:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setKhtpname("不定");
			for (int i = 0; i < merchantTypelist.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setKhtpname(merchantTypelist.get(i).getKstype());
			break;
		case 7:
			obj.setKhtel(String.valueOf(value));
			break;
		case 8:
			obj.setKhaddress(String.valueOf(value));
			break;
		case 9:
			obj.setPeople(String.valueOf(value));
			break;
		case 10:
			obj.setFax(String.valueOf(value));
			break;
		case 11:
			obj.setYzbm(String.valueOf(value));
			break;
		case 12:
			obj.setMail(String.valueOf(value));
			break;
		case 13:
			obj.setYh(String.valueOf(value));
			break;
		}
		getViewer().update(element, null);
	}

	@Override
	public void changeEvent() {
		if (editor instanceof ClientInfoTextKeyCellEditor)
			((ClientInfoTextKeyCellEditor) editor).releaseKey();
		if (editor instanceof ClientInfoComboBoxCellEditor)
			((ClientInfoComboBoxCellEditor) editor).releaseKey();
	}

	@Override
	public void modify() {
		isFirst = true;
	}

}
